package com.baobaoyeye.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baobaoyeye.conn.*;
/**
 * Servlet implementation class checklogin
 */
public class Checklogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ConnMySQL connSql = null;   
    private ResultSet rs = null;
	private String _user_id;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Checklogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("username");
		String pass = request.getParameter("password");
		String type = request.getParameter("usertype");
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		connSql = new ConnMySQL();
		int flag = -1;
		//check usertype
		if(type.equals("Student")|| type=="Student"){
			if(CheckStudent(name,pass)){
				flag = 1;
			}
			else{
				GoBackPage();
			}
		}else if(type.equals("Teacher")|| type == "Teacher"){
			if(CheckTeacher(name,pass)){
				flag = 2;
			}
			else{
				GoBackPage();
			}
		}else if(type.equals("Admin") || type=="Admin"){
			
			if(CheckAdmin(name,pass)){
				flag = 3;
			}
			else{
				GoBackPage();
			}
		}else{
			GoBackPage();
		}
		if( flag !=-1){
			HttpSession session =request.getSession(true);
			if(_user_id!=null && _user_id.length()>0){
				session.setAttribute("_type",type);
				session.setAttribute("_user_id",_user_id);
			}
		}
        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        String newLocn = "index.jsp";
        if(flag == 3){
        	newLocn="adminplace.jsp";
        }else if(flag == 2){
        	newLocn = "teacherplace.jsp";
        }else if(flag == 1){
        	newLocn = "studentplace.jsp";
        }
        response.setHeader("Location", newLocn);
	}

	private void GoBackPage() {
		// TODO Auto-generated method stub
		
	}

	private boolean CheckAdmin(String name, String pass) {
		// TODO Auto-generated method stub
		String sql = "select admin_id,admin_pass from admin where admin_name='"+name+"';";
		rs = connSql.executeQuery(sql);
		boolean flag = false;
		try {
			while(rs.next()){
				_user_id = rs.getString(1);
				String db_adminpass = rs.getString(2);
				if(db_adminpass.equals(pass)||db_adminpass == pass){
					flag = true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			connSql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag == true)
			return true;
		else
			return false;
	}

	private boolean CheckTeacher(String name, String pass) {
		String sql = "select teacher_id,teacher_pass from teacher where teacher_name='"+name+"';";
		rs = connSql.executeQuery(sql);
		boolean flag = false;
		try {
			while(rs.next()){
				_user_id = rs.getString(1);
				String db_teacherpass = rs.getString(2);
				if(db_teacherpass.equals(pass)||db_teacherpass == pass){
					flag = true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			connSql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag == true)
			return true;
		else
			return false;
	}

	private boolean CheckStudent(String name, String pass) {
		String sql = "select student_id,student_pass from student where student_name='"+name+"';";
		rs = connSql.executeQuery(sql);
		boolean flag = false;
		try {
			while(rs.next()){
				_user_id = rs.getString(1);
				String db_studentpass = rs.getString(2);
				if(db_studentpass.equals(pass)||db_studentpass == pass){
					flag = true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			connSql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag == true)
			return true;
		else
			return false;
	}

}
